﻿//===================================RandomNoiseALgorithm.cs============================\\
//===Mark Wallenburg======\\


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Collections;


namespace Team2_Vision
{
    class RandomNoise : VisionAlgorithm
    {
        public RandomNoise(String name) : base(name) { }
        public override System.Drawing.Bitmap DoAlgorithm(System.Drawing.Bitmap sourceImage, int intensity)
        {
            Bitmap returnImage = new Bitmap(sourceImage.Width, sourceImage.Height);
            BitmapData sourceImageData = sourceImage.LockBits(new Rectangle(0, 0, sourceImage.Width, sourceImage.Height), ImageLockMode.ReadWrite, sourceImage.PixelFormat);
            BitmapData returnImageData = returnImage.LockBits(new Rectangle(0, 0, returnImage.Width, returnImage.Height), ImageLockMode.ReadWrite, sourceImage.PixelFormat);

            IntPtr adressreturn = returnImageData.Scan0;
            IntPtr adresssource = sourceImageData.Scan0;

            int arraySize = Math.Abs(sourceImageData.Stride) * sourceImageData.Height;

            int Sizemap = ((sourceImage.Width * sourceImage.Height) / 500);
            int[,] matrix = new int[2, 2];
            int[,][] img = new int[sourceImage.Width + 1, sourceImage.Height + 1][];
            byte[] array = new byte[arraySize];
            int counter = 0;
            Random r = new Random();

            Marshal.Copy(adresssource, array, 0, arraySize);


            for (int x = 0; x < sourceImage.Width; x++)
            {
                for (int y = 0; y < sourceImage.Height; y++)
                {
                    img[x, y] = new int[3];
                    for (int i = 0; i < 3; i++)
                    {
                        img[x, y][i] = array[counter];
                        counter++;
                    }
                }
            }

            for (int i = 0; i < 2; i++)
            {
                for (int i2 = 0; i2 < 2; i2++)
                {
                    matrix[i, i2] = Convert.ToInt32(r.NextDouble());
                }
            }

            for (int i = 0; i <= intensity*Sizemap; i++)
            {
                int x = r.Next(1, returnImage.Width - 1);
                int y = r.Next(1, returnImage.Height - 1);
                addNoise(img, matrix, x, y, returnImage.Height, returnImage.Width);
            }
            counter = 0;
            for (int x = 0; x < sourceImage.Width; x++)
            {
                for (int y = 0; y < sourceImage.Height; y++)
                {
                    for (int i = 0; i < 3; i++)
                    {
                        array[counter] = (byte)img[x, y][i];
                        counter++;
                    }
                }
            }

            Marshal.Copy(array, 0, adressreturn, arraySize);
            sourceImage.UnlockBits(sourceImageData);
            returnImage.UnlockBits(returnImageData);
            return returnImage;
        }



        public static void addNoise(int[,][] img, int[,] matrix, int x, int y, int height, int width)
        {
            Random r = new Random();
            if (x > 0 && x < width && y > 0 && y < height)
            {
                int i = 0, ammount = 0;
                i = r.Next(0, 2);
                ammount = r.Next(1, 50);
                img[x, y][i] = img[x, y][i] + ammount;
                i = r.Next(0, 2);
                img[x - 1, y][i] = img[x - 1, y][i] + ammount;
                i = r.Next(0, 2);
                img[x, y - 1][i] = img[x, y - 1][i] + ammount;
                i = r.Next(0, 2);
                img[x - 1, y - 1][i] = img[x - 1, y - 1][i] + ammount;

            }
            else
            {
                int newX = r.Next(1, width - 1);
                int newY = r.Next(1, height - 1);
                addNoise(img, matrix, newX, newY, height, width);
            }
        }
    }
}

